/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.onedec.project.serviceImpl;

import com.onedec.project.entity.OnedecProject;
import com.onedec.project.entity.OnedecProjectExample;
import com.onedec.project.mapper.OnedecProjectMapper;
import com.onedec.project.service.ProjectService;
import com.onedec.utils.PageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 31014
 * @date 2020/7/16 23:17
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private OnedecProjectMapper onedecProjectMapper;

    public void insertProject(OnedecProject onedecProject) {
        onedecProjectMapper.insertSelective(onedecProject);
    }

    @Override
    public PageBean<OnedecProject> selectProjectList(Map<String, Object> params) {
        final List<OnedecProject> projects = onedecProjectMapper.selectByPage(params);
        final PageBean<OnedecProject> pageBean = new PageBean<>();
        pageBean.setTotalNum((int) onedecProjectMapper.selectCountPage(params));
        pageBean.setData(projects);
        return pageBean;
    }

    public void updateProjectByDesigner(Integer eId, String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        project.setProjectStylistId(eId);
        project.setProjectInPhase(2);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    public void updateProjectByDrawings(Integer eId, String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        project.setProjectInPhase(3);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    public void updateAuditDrawing(Integer eId, String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        project.setProjectInPhase(4);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    public void addManager(Integer eId, String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        project.setProjectPmId(eId);
        project.setProjectInPhase(5);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    public void addAssign(Integer eId, String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        project.setProjectSupervisorId(eId);
        project.setProjectInPhase(7);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    @Override
    public void updateStatus(Integer eId, String projectNo, Integer status) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final OnedecProject project = new OnedecProject();
        if (status == 27) {
            project.setProjectSts(2);
        }
        project.setProjectInPhase(status);
        onedecProjectMapper.updateByExampleSelective(project, example);
    }

    public OnedecProject selectProjectByProjectNo(String projectNo) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(projectNo);
        final List<OnedecProject> projects = onedecProjectMapper.selectByExample(example);
        return projects.get(0);
    }
}